package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserinfoService;
import com.tanhua.sso.vo.ErrorResult;
import com.tanhua.sso.vo.PicUploadResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("user")
public class UserInfoController {

        @Autowired
        UserinfoService userinfoService;

        @PostMapping("loginReginfo")
        public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String,String> param,
                                                   @RequestHeader("Authorization") String token){

            boolean userInfo = userinfoService.saveUserInfo(param, token);
            try {
                //如果为true，返回200
                if (userInfo){
                    return ResponseEntity.ok(null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            ErrorResult build = ErrorResult.builder().errCode("000001").errMessage("保存用户信息失败！").build();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(build);
        }

        @PostMapping("loginReginfo/head")
        public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto") MultipartFile file,
                                                   @RequestHeader("Authorization") String token){
            boolean b = userinfoService.saveUserLogo(file, token);
            try {
                if (b){
                    //如果为true那么就返回200，保存成功
                    return ResponseEntity.ok(null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            ErrorResult build = ErrorResult.builder().errCode("000001").errMessage("保存用户头像失败！").build();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(build);
        }
}
